// xc_lshop/club/order/orderlist.js
var util = require('../../../utils/util.js')
var actclub_manager = require('../model/club_manage')
var actbill = require('../model/club_bill')
Page({
  mixins: [require('../mixins/clubMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    actmodel: null,
    openstatus: false, //选择框显示否
    option: [{
      text: '全部日期',
      value: 0
    }, {
      text: '固定日期',
      value: 1
    }], //选择选项
    value: 0, //选择值
    searchmodel: { //查询条件
      seachkey: "",
      navcur: 0, //导航栏index
      type: 0, //0全部日期 1，固定日期
    },
    modellist: [],
    result: [], //多选id值
    show: false, //核销信息
    checked: false, //全选
    checkedids:[]
  },
  afterclubinfo: function (options, config, userinfo, clubinfo) {
    this.setData({
      clubinfo:clubinfo
    })
    if(config.yihuo){
      this.setData({
        yihuoconfig:config.yihuo
      })
    }

    if (options.opstatus) {

      this.setData({
        'searchmodel.navcur': parseInt(options.opstatus)
      })

    }

    var strwhere = {}
    var that = this
    that.getpagelist(that, 1, strwhere)

  },
  getwhere() {
    var strwhere = {};
    var searchmodel = this.data.searchmodel
    strwhere["opstatus"] = this.data.searchmodel.navcur
    if (searchmodel.type == 1) {
      if (searchmodel.startdate) {
        strwhere["startdate"] = searchmodel.startdate
      }
      if (searchmodel.enddate) {
        strwhere["enddate"] = searchmodel.enddate
      }

    }
    if (searchmodel["seachkey"]) {
      strwhere["seachkey"] = searchmodel["seachkey"]
    }
    return strwhere
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      strwhere = this.getwhere();
      // if(!strwhere)
      // {
      //   strwhere={}
      // }
      // strwhere["status"] = this.data.activetab
      that.setData({
        modellist: [],
        isempty: false
      });
    }
    actbill.offset(that, rest, strwhere).then(function (result) {
      var updatemodel = {};
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
        if (result["obj"]["modellist"].length < 1 && that.data.modellist.length < 1) {
          updatemodel["isempty"] = true
        } else if (rest == 1) {
          updatemodel["modellist"] = result["obj"]["modellist"];
        } else {
          if (result["obj"]["modellist"].length > 0) {
            updatemodel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      // that.selectComponent("#ordercomment").showmodel( updatemdoel["modellist"][0],"order")      
      wx.stopPullDownRefresh();
      that.setData(updatemodel);
    }, function () {

    });
  },
  //全选
  onCheckAllChange(e) {
    console.log("全选")
    console.log(e)
    var value=this.data.checked
    this.setData({
      checked: !value,
    });
    this.keepcheck(!value)
  },
  //选择
  onCheckChanges(e){
    console.log(e)
    var index=e.currentTarget.dataset.index;
    var name='modellist['+index+'].checked';
    var value=this.data.modellist[index].checked;
  
    this.setData({
      [name]:!value
    })
    this.keepcheck(-1)
  },
  //监听选择
  keepcheck(allchecked){
     var modellist=this.data.modellist;
     var ids=[];
     for(var i in modellist){
       if(allchecked==-1){
        if(modellist[i].checked){
          ids.push(modellist[i].id)
        }
       }else{
        if(allchecked){
          modellist[i].checked=allchecked
          ids.push(modellist[i].id)
        }else{
          modellist[i].checked=allchecked
        }
       }
     }
     this.setData({
      checkedids:ids,
      modellist:modellist
     })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    that.getpagelist(that, 1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.getpagelist(that, -1)
  },


  changeseach(e) {
    var that = this
    this.setData({
      'searchmodel.seachkey': e.detail
    })
    that.getpagelist(that, 1)
  },
  // 切换日期--开始
  onChange: function (e) {
    this.setData({
      'searchmodel.type': e.detail
    })
    var that = this;
    that.getpagelist(that, 1)
  },
  menuOpen: function (e) {
    this.setData({
      openstatus: true
    })
  },
  menuClose: function (e) {
    this.setData({
      openstatus: false
    })
  },
  bindDateChange: function (e) {
    var that = this
    var val = e.currentTarget.dataset.val;
    var updatemodel = {};
    if (val == "start") {
      updatemodel['searchmodel.startdate'] = e.detail.value


    } else if (val == "end") {

      updatemodel['searchmodel.enddate'] = e.detail.value

    }
    this.setData(updatemodel)
    that.getpagelist(that, 1)
  },
  optool(e) {
    var dataset = e.currentTarget.dataset
    var that = this
    var checkedids=[]
    if(dataset.id){
      checkedids.push(dataset.id)
    }else if(this.data.checkedids){
      checkedids=this.data.checkedids
    }
    if(checkedids.length<=0){
      return;
    }

    actbill.optool(dataset.op, checkedids).then(res => {
      if (res["status"] == 1) {
        wx.showToast({
          title: '操作成功',
        })
        that.getpagelist(that, 1)
        if (that.data.actmodel) {
          var udpatemdoel = {}
          switch (dataset.op) {
            case "delivery": {
              udpatemdoel["actmodel.clubstatus"] = "3"

              break;
            }
            case "arrival": {
              udpatemdoel["actmodel.status"] = "6"
              if (res["obj"]) {
                udpatemdoel["actmodel.clubop"] = res["obj"];
              }
              break;
            }
            case "writeoff": {
              udpatemdoel["actmodel.status"] = "9"
              if (res["obj"]) {
                udpatemdoel["actmodel.clubop.writeoffname"] = res["obj"]["writeoffname"];
                udpatemdoel["actmodel.clubop.writeofftime"] = res["obj"]["writeofftime"];
              }
              break;
            }
          }
          that.setData(udpatemdoel)
        }


      } else {
        wx.showModal({
          title: res["message"] ? res["message"] : '操作失败'
        })
      }
    })




  },
  // 切换日期--结束
  //切换导航
  onNavChange(e) {
    this.setData({
      checked:false,
      'searchmodel.navcur': e.detail.index
    })
    var that = this
    this.keepcheck(false)
    that.getpagelist(that, 1)
  },
  //切换订单选择
  onChooseChange(event) {
    this.setData({
      result: event.detail,
    });
  },
  onPopShow: function (e) {
    var dataset = e.currentTarget.dataset
    this.setData({
      actmodel: dataset.item,
      show: true
    })

    return;

  },
  onPopClose: function () {
    this.setData({
      actmodel: null,
      show: false
    })
  },
  
})